use glam::{Mat4, Quat, Vec3, Vec4, vec4};
use glium::winit::keyboard::{self, NamedKey};

pub struct Camera {
    pub position: Vec3,
    pub center: Vec3,
    pub up: Vec3,
    pub fov: f32,
    pub aspect_ration: f32,
    pub take_snap: bool,
}

impl Camera {
    pub fn view_mat(&self) -> Mat4 {
        Mat4::look_at_rh(self.position, self.center, self.up)
    }

    pub fn perspective_mat(&self) -> Mat4 {
        Mat4::perspective_rh(self.fov, self.aspect_ration, 0.01, 100.)
    }

    pub fn view_port_mat(width: f32, height: f32) -> Mat4 {
        Mat4::from_cols(
            vec4(width / 2., 0., 0., width / 2.),
            vec4(0., height / 2., 0., height / 2.),
            Vec4::Z,
            Vec4::W,
        )
        .transpose()
    }

    pub fn keyboard_respond(&mut self, key: &keyboard::NamedKey) {
        match key {
            NamedKey::ArrowUp => self.up = Quat::from_rotation_z(0.01) * self.up,
            NamedKey::ArrowDown => self.up = Quat::from_rotation_z(-0.01) * self.up,
            NamedKey::ArrowRight => self.position = Quat::from_rotation_y(0.01) * self.position,
            NamedKey::ArrowLeft => self.position = Quat::from_rotation_y(-0.01) * self.position,
            NamedKey::PageUp => self.position *= 0.99,
            NamedKey::PageDown => self.position *= 1.01,
            _ => {}
        }
    }
}
